package com.bdy.lm.taximanager.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.bdy.lm.taximanager.R;
import com.bdy.lm.taximanager.model.DriverInfo;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class MessageListActivity extends Activity {

    private RefreshableView refreshableView;
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, String>> msgList;
    private DriverInfo driverInfo;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_list);

        msgList = (List<Map<String, String>>) getIntent().getExtras().getSerializable("msgList");
        driverInfo = DriverInfo.getDriverInfo();

        listView = (ListView) findViewById(R.id.list_view);
        simpleAdapter = new SimpleAdapter(this, msgList, R.layout.simple_item, new String[]{"title", "date"},
                new int[]{R.id.message_title, R.id.message_date});
        refreshableView = (RefreshableView) findViewById(R.id.refreshable_view);
        listView.setAdapter(simpleAdapter);

        handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 101) {
                    simpleAdapter.notifyDataSetChanged();
                }
            }
        };

        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    Log.d("PullToRefresh", "下拉回调");
                    if (driverInfo.getMsgList().size() > msgList.size()) {
                        msgList.clear();
                        msgList.addAll(driverInfo.getMsgList());
                        handler.sendEmptyMessage(101);
                    }
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                refreshableView.finishRefreshing();
            }
        }, 0);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MessageListActivity.this, MessageContentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("msgLoc", position);
                bundle.putSerializable("msgList", (Serializable) msgList);
                intent.putExtras(bundle);
                startActivity(intent);
                System.out.println("第" + position + "被点击");
            }
        });
    }
}
